/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapper;

import beans.AccountBean;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Haomaru
 */
public class AccountMapper extends DBMapper {

    public AccountMapper() throws Exception {
        super();
    }

    public boolean login(String ID, String Pass) throws Exception {
        boolean found = false;
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM Account WHERE ");
            sql.append(" ID = '" + ID + "'");
            sql.append(" AND Pass = '" + Pass + "'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            if ((rs != null) && rs.next()) {
                found = true;
            }

            stmt.close();
        } catch (Exception e) {
            throw e;
        }

        return found;
    }

    public AccountBean getUserInformation(String ID) throws Exception {
        AccountBean accBean = new AccountBean();

        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM Account WHERE ");
            sql.append(" ID = '" + ID + "'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            if ((rs != null) && rs.next()) {
                initAccountBean(accBean, rs);
            }

            stmt.close();
        } catch (Exception e) {
            throw e;
        }

        return accBean;
    }

    public void insertNewUser(AccountBean account) throws Exception {
        try {
            StringBuffer sql = new StringBuffer();
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            //...
            stmt.execute();
            stmt.close();
        } catch (Exception e) {
            throw e;
        }
    }

    public ArrayList<AccountBean> listUser() throws Exception {
        ArrayList<AccountBean> listOfUsers = new ArrayList<AccountBean>();
        AccountBean UserBean = null;
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM Account");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while ((rs != null) && rs.next()) {
                UserBean = new AccountBean();
                initAccountBean(UserBean, rs);
                listOfUsers.add(UserBean);
            }

            stmt.close();
        } catch (Exception e) {
            throw e;
        }

        return listOfUsers;
    }

    public void initAccountBean(AccountBean accBean, ResultSet rs) throws Exception {
        if ((rs != null) && (accBean != null)) {
            accBean.setID(rs.getString("id"));
            accBean.setPass(rs.getString("pass"));
            accBean.setName(rs.getString("name"));
            accBean.setType("type");
            accBean.setEmail(rs.getString("email"));
            accBean.setAddress(rs.getString("address"));
            accBean.setPhone(rs.getString("phone"));
            accBean.setRole(rs.getString("role"));
        }
    }
}
